package com.irate.fragment;

import java.io.Serializable;

import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;

import com.irate.activity.R;
import com.irate.fragment.FriendViewFriend.updateUI;
import com.irate.provider.ContentManager;

public class FriendFragment extends Fragment implements updateUI, Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Fragment friendViewFriend;
	private Fragment friendAddFriend;
	private Fragment friendRequestFriend;

	private TextView viewFriend;
	private TextView addFriend;
	private TextView requestFriend;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return inflater.inflate(R.layout.friend_layout, null);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		init();
		execute();
	}

	private void execute() {

		int selectedLayout = ContentManager.getInstance()
				.getCurrentFriendLayoutSelected();
		switch (selectedLayout) {
		case 0:
			setUpViewFriend();
			break;
		case 1:
			String str00 = viewFriend.getText().toString();

			viewFriend.setText(str00 + " \n("
					+ ContentManager.getInstance().getNumOfFriend() + ")");
			setupAddFriend();
			break;
		case 2:
			String str01 = viewFriend.getText().toString();

			viewFriend.setText(str01 + " \n("
					+ ContentManager.getInstance().getNumOfFriend() + ")");
			setupRequestFriend();
			break;

		default:
			break;
		}

		viewFriend.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				ContentManager.getInstance().setCurrentFriendLayoutSelected(0);
				setUpViewFriend();

			}

		});

		addFriend.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				ContentManager.getInstance().setCurrentFriendLayoutSelected(1);
				setupAddFriend();
			}

		});

		requestFriend.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				ContentManager.getInstance().setCurrentFriendLayoutSelected(2);
				setupRequestFriend();
			}

		});
	}

	private void setUpViewFriend() {
		viewFriend.setSelected(true);
		addFriend.setSelected(false);
		requestFriend.setSelected(false);

		getActivity().getSupportFragmentManager().beginTransaction()
				.replace(R.id.content_friend, friendViewFriend).commit();
	}

	private void setupRequestFriend() {
		viewFriend.setSelected(false);
		addFriend.setSelected(false);
		requestFriend.setSelected(true);

		getActivity().getSupportFragmentManager().beginTransaction()
				.replace(R.id.content_friend, friendRequestFriend).commit();
	}

	private void setupAddFriend() {
		viewFriend.setSelected(false);
		addFriend.setSelected(true);
		requestFriend.setSelected(false);

		getActivity().getSupportFragmentManager().beginTransaction()
				.replace(R.id.content_friend, friendAddFriend).commit();
	}

	private void init() {
		friendViewFriend = new FriendViewFriend();

		// Bundle bundle = new Bundle();
		// bundle.putSerializable("parent", this);
		// friendViewFriend.setArguments(bundle);

		ContentManager.getInstance().setFrienFragment(this);

		friendAddFriend = new FriendAddFriend();
		friendRequestFriend = new FriendRequestFriend();

		viewFriend = (TextView) getActivity().findViewById(
				R.id.friend_view_friend);
		addFriend = (TextView) getActivity().findViewById(
				R.id.friend_add_friend);
		requestFriend = (TextView) getActivity().findViewById(
				R.id.friend_request_friend);
	}

	@Override
	public void onUpdate(int i) {

		try {
			viewFriend.setText(getResources().getString(R.string.friend)
					+ "\n(" + i + ")");
			((View) viewFriend.getParent()).invalidate();

			ContentManager.getInstance().setNumOfFriend(i);
		} catch (Exception e) {

		}
	}

}
